<?php
function processCart ($gParams) {
	global $modx;
	
	//Set default templates
	if(!$gParams['gCartTpl']) $gParams['gCartTpl'] = '[+gWrap+]
								<p><a href="[+gClearCartURL+]">Clear Cart</a></p>
								<p>Total Items: [+gTotalItems+]</p>
								<p>Subtotal: $[+gSubtotal+]</p>
								<p>Discount: $[+gDiscount+]</p>
								[+gButton+]';
								
	
	if(!$gParams['gAjaxTpl']) $gParams['gAjaxTpl'] = '<div id="highlight">[+gAjax+]</div>';
	if(!$gParams['gEmptyTpl']) $gParams['gEmptyTpl'] = '<div id="minicart"><h1>Your Cart</h1><p>Your cart is empty.</p></div>';
	
	if(!$gParams['gItemTpl']) $gParams['gItemTpl'] = '<p>
									<a href="[+gClearItemURL+]">
										<img alt="Remove Item" class="right" height="11" src="http://www.cmssandbox.com/MODx095/assets/images/list_remove_btn.gif" title="Remove Item" width="11" />
									</a>
									<a href="[+gItemURL+]">[+gName+]</a><br />
									<strong>Qty: [+gQuantity+]</strong>
								</p>';
	
	if($_SESSION["tempCart"]) {
		//This information is located by logging into your sellers account.
		$merchant_id = $gParams['gID'];
		$merchant_key = $gParams['gKey'];
		
		//Create a new cart object and setup its basic options
		$gCart = new gCart($merchant_id, $merchant_key);
		$gTaxRule = new gTaxRule($gParams['gTaxRate'],'ALL');
		$gTaxRule->setShippingTaxed($gParams['gTaxShipping']);
		$gTaxTable = new gTaxTable('Default',array($gTaxRule),'default');
		$gAltTaxTable = new gTaxTable('tax_exempt','','alternate','true');
		$gCart->setDefaultTaxTable($gTaxTable);
		$gCart->setAlternateTaxTables(array($gAltTaxTable));
		
		//Shipping Options
		$gShippingOption1 = new gShippingOption('20.00','UPS','REGULAR_PICKUP','Ground','3.00');
		$gShippingOption2 = new gShippingOption('40.00','UPS','REGULAR_PICKUP','3 Day Select','3.00');
		$gShippingOption3 = new gShippingOption('60.00','UPS','REGULAR_PICKUP','Next Day Air','3.00');
		$gShippingOption = array($gShippingOption1,$gShippingOption2,$gShippingOption3);
		$gShippingPackage = new gShippingPackage($gParams['gShipID'],$gParams['gZipCode'], $gParams['gCity'], $gParams['gState'],'US','17','17','17');
		$gShipping = array(new gShipping('Standard','10','carrier-calculated-shipping',$gShippingOption, $gShippingPackage));
		

		//Add the items from the tempCart into the cart being passed to Google. 
		foreach($_SESSION["tempCart"] as $item) {
			
			//Check if item if tax-exempt.
			if ($item['taxstatus'] != 'Yes') {
				$gCart->addItem($item['name'],$item['description'],$item['quantity'],$item['price'],$item['weight']);
				} else
					$gCart->addItem($item['name'],$item['description'],$item['quantity'],$item['price'],$item['weight'],'tax_exempt');
			
			//Check if all items in the cart are eligible for free shipping.
			if ($item['freeship'] != 'Yes')
				$charge = true;
				
			$output = str_replace('[+gName+]',$item['name'],$gParams['gItemTpl']);
			$output = str_replace('[+gSKU+]',$item['sku'],$output);
			$output = str_replace('[+gAlias+]',$item['alias'],$output);
			$output = str_replace('[+gQuantity+]',$item['quantity'],$output);
			$output = str_replace('[+gItemURL+]',$item['sku'],$output);
			$output = str_replace('[+gItemTotal+]',number_format($item['price']*$item['quantity'],2),$output);
			$output = str_replace('[+gClearItemURL+]',$modx->makeURL($modx->documentIdentifier).'&amp;clearItem='.$item['sku'],$output);
			$output = str_replace('[+gIncrementURL+]',$modx->makeURL($modx->documentIdentifier).'&amp;increment='.$item['sku'],$output);
			$output = str_replace('[+gDecrementURL+]',$modx->makeURL($modx->documentIdentifier).'&amp;decrement='.$item['sku'],$output);
			$output = str_replace('[+gPrice+]',$item['price'],$output);
			$finalOutput .= $output;
			}
			
			//Output the items wrapped inside the cart
			$estimatedTax = $gParams['gTaxRate'] * $_SESSION["gCartInfo"]['subtotal'];
			$finalOutput = str_replace('[+gWrap+]',$finalOutput,$gParams['gCartTpl']);
			$finalOutput = str_replace('[+gClearCartURL+]',$modx->makeURL($modx->documentIdentifier).'&amp;clearCart=1',$finalOutput);
			$finalOutput = str_replace('[+gTotalItems+]',$_SESSION["gCartInfo"]['quantity'],$finalOutput);
			$finalOutput = str_replace('[+gWeight+]',$_SESSION["gCartInfo"]['weight'],$finalOutput);
			$finalOutput = str_replace('[+gError+]',$_SESSION["error"],$finalOutput);
			$finalOutput = str_replace('[+gSubtotal+]',number_format($_SESSION["gCartInfo"]['subtotal'],2),$finalOutput);
			$finalOutput = str_replace('[+gEstimatedTax+]',number_format($estimatedTax,2),$finalOutput);
			$finalOutput = str_replace('[+gDiscount+]',number_format($_SESSION["gTotalDiscounts"],2),$finalOutput);
			$finalOutput = str_replace('[+gEncodedCart+]',$gCart->getEncodedCart(),$finalOutput);
			$finalOutput = str_replace('[+gEncodedSig+]',$gCart->getEncodedSignature(),$finalOutput);
			if($_POST['gName']) $finalOutput = str_replace('[+gAjax+]',$finalOutput,$gParams['gAjaxTpl']);
		
		//All Items in the cart are eligible for free shipping.
		if($charge) $gCart->setShipping($gShipping);
		
		//Add all your discounts by calling the functions created in the discount file
		addTieredDiscount('.1,.15,.2','11,23,47','wine','Bulk Discount');
		addAgeFee('3.25','wine','Age Verification','Your shipment contains wine and requires the UPS driver to check identification.');
		
		//Add the discounts into the cart being passed to Google
		$_SESSION["gTotalDiscounts"] = 0;
		foreach($_SESSION["gDiscounts"] as $item) {
			$gCart->addItem($item['name'],$item['description'],$item['quantity'],$item['price'],$item['weight']);
			$_SESSION["gTotalDiscounts"] += $item['price'];
			}
		
		//Add the fees into the cart being passed to Google
		
		$_SESSION["gTotalFees"] = 0;
		foreach($_SESSION["gFees"] as $item) {
			$gCart->addItem($item['name'],$item['description'],$item['quantity'],$item['price'],$item['weight']);
			$_SESSION["gTotalFees"] += $item['price'];
			}
		
		//Google checkout only supports a shipment of less than 150lbs.
		if($_SESSION["gCartInfo"]['weight'] <= 150){ 
			//Generate the checkout button
			$gButton = new gButton($gCart,'SMALL');
			$button = $gButton->getPostButton();
			$finalOutput = str_replace('[+gButton+]',$button,$finalOutput);
			
			}
			else
			$finalOutput = str_replace('[+gButton+]','<p>Your cart is too heavy.  Try removing one item and <a href="' . $modx->makeURL($modx->documentIdentifier) . '">clicking here</a>.</p>',$finalOutput);
		
		return $finalOutput; 
	} else {
	return $gParams['gEmptyTpl'];
	}
}

?>